package View;

/**
 * This class inherits the JFrame populates the frame with the general view
 * of the building such as the floors, the elevators and their status
 * It also contains the inside view of the elevator panel such as the floor buttons
 * Each floor has an up and down button for the floor calls by the user
 * 
 * @author Varun Maryada, Sanat Dixit, Steve Tannahill
 */

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import Dispatcher.GroupDispatcher;
import Dispatcher.HandleReceivedMsgThread;
import Dispatcher.UpdateThread;

public class BuildingGUI extends JFrame implements ActionListener{
	
	private static final int FLOORS = 15;
	private static final int ELEVATORS = 4;
	
	//FLOORS is actual number of floors
	//GUI has FLOORS+1.. Top row is for labels
	//FLOORS+1-row is to access current floor
	
	private ElevatorGUI elePanel;
	private JTextArea console;
	
	private GroupDispatcher gd;
	
	/**
	 * Constructs the building view containing floors and inside view of elevators
	 * @param gd
	 */
	public BuildingGUI(GroupDispatcher gd){
		super("Elevator Control System");
		
		this.gd = gd;
		
		this.setSize(1000, 700);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.buildGrid();
	}
	
	/**
	 * Builds a grid layout
	 */
	public void buildGrid(){
		
		Container cp = this.getContentPane();
		cp.setLayout(new BorderLayout());
		JPanel grid = new JPanel();
		grid.setLayout(new GridLayout(FLOORS+1, ELEVATORS+2));
		cp.add(grid, BorderLayout.CENTER);
		
		for (int i = 0; i < 16; i++) {
			if (i == 0) { this.filltopLabel(grid, i); }
			else{
				this.fillGrid(grid, i);
			}
		}
		this.addElevatorPanel(cp);
	}
	
	/**
	 * Adds the Elevators and floor numbers to the Content Pane
	 * @param grid
	 * @param row
	 */
	public void fillGrid(Container grid, int row) {
		int floorNumber = FLOORS+1-row;
		//floor number
		grid.add(new JLabel(""+floorNumber, JLabel.CENTER));
		//floor buttons
		grid.add(new FloorGUI(floorNumber, gd));
		//add elevators
		if (floorNumber == 1) {
			grid.add(new JLabel("X", JLabel.CENTER));
			grid.add(new JLabel("X", JLabel.CENTER));
			grid.add(new JLabel("X", JLabel.CENTER));
			grid.add(new JLabel("X", JLabel.CENTER));
		}
		else {
			grid.add(new JLabel("", JLabel.CENTER));
			grid.add(new JLabel("", JLabel.CENTER));
			grid.add(new JLabel("", JLabel.CENTER));
			grid.add(new JLabel("", JLabel.CENTER));
		}
	}
	
	/**
	 * Creates the Labels and adds them to the grid
	 * @param grid
	 * @param row
	 */
	public void filltopLabel(Container grid, int row){
		grid.add(new JLabel("Floors", JLabel.CENTER));
		grid.add(new JLabel("", JLabel.CENTER));
		grid.add(new JLabel("Elevator-1", JLabel.CENTER));
		grid.add(new JLabel("Elevator-2", JLabel.CENTER));
		grid.add(new JLabel("Elevator-3", JLabel.CENTER));
		grid.add(new JLabel("Elevator-4", JLabel.CENTER));
	}
	
	/**
	 * Adds the inside view of the elevator panel to the grid
	 * @param cp
	 */
	public void addElevatorPanel(Container cp){
		elePanel = new ElevatorGUI(1, gd);
		JPanel elevatorsPanel = new JPanel();
		elevatorsPanel.setLayout(new GridLayout(4,1));
		elevatorsPanel.add(elePanel, 0, 0);
		cp.add(elevatorsPanel, BorderLayout.EAST);
	}
	
	public void addConsole(Container cp){
		console = new JTextArea("Console", 5, 70);
		JPanel consolePanel = new JPanel();
		consolePanel.setLayout(new BorderLayout());
		JScrollPane scrollingArea = new JScrollPane(console);
		scrollingArea.setVerticalScrollBarPolicy(
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		consolePanel.add(scrollingArea, BorderLayout.CENTER);
		
		console.setBorder(BorderFactory.createLineBorder(Color.black));
		cp.add(consolePanel, BorderLayout.SOUTH);
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		GroupDispatcher gd = new GroupDispatcher();
		
		BuildingGUI building = new BuildingGUI(gd);
		building.setVisible(true);
		
		String msg;
		System.out.println("Started dispatcher");
		// create and start an update thread if needed
		UpdateThread ut = new UpdateThread(gd);
		ut.start();
		gd.activateSocket();
		while (true) {
			msg = gd.receiveMessage();
			HandleReceivedMsgThread hrm = new HandleReceivedMsgThread(gd, msg);
			hrm.start();
		}
	}

	
}
